package com.bzsg.rightsManagementSystem.dao

import com.bzsg.rightsManagementSystem.pojo.Permission
import org.apache.ibatis.annotations.Delete
import org.apache.ibatis.annotations.Insert
import org.apache.ibatis.annotations.Mapper
import org.apache.ibatis.annotations.Select
import org.apache.ibatis.annotations.Update


@Mapper
interface PermissionDao {
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Select("select *  from permission where id = #{id}")
    fun queryById(id: Int): Permission?

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Select("    select * from permission limit #{offset}, #{limit}")
    fun queryAllByLimit(offset: Int, limit: Int): List<Permission>?

    /**
     * 查询所有权限
     *
     * @param
     * @return 权限列表
     */
    @Select("select * from permission")
    fun queryAll(): List<Permission>?

    /**
     * 新增数据
     *
     * @param permission 实例对象
     * @return 实例对象
     */

    @Insert(
        "  insert into permission(permission_code, permission_name)\n" +
                "        values (#{permissionCode}, #{permissionName})"
    )
    fun insert(permission: Permission)

    /**
     * 修改数据
     *
     * @param permission 实例对象
     * @return 实例对象
     */
    @Update(
        "  <script>\n" +
                "        update permission\n" +
                "        <set>\n" +
                "            <if test=\"permissionCode != null and permissionCode != ''\">\n" +
                "                permission_code = #{permissionCode},\n" +
                "            </if>\n" +
                "            <if test=\"permissionName != null and permissionName != ''\">\n" +
                "                permission_name = #{permissionName},\n" +
                "            </if>\n" +
                "        </set>\n" +
                "        where id = #{id}\n" +
                "  </script>"
    )
    fun update(permission: Permission)

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Delete("delete from permission where id = #{id}")
    fun deleteById(id: Int)

    /**
     * 查询用户的权限列表
     *
     * @param userId
     * @return 权限列表
     */
    @Select(
        "  SELECT p.*\n" +
                "        FROM personUser AS u\n" +
                "                 LEFT JOIN user_role_relation AS ur\n" +
                "                           ON u.id = ur.user_id\n" +
                "                 LEFT JOIN role AS r\n" +
                "                           ON r.id = ur.role_id\n" +
                "                 LEFT JOIN role_permission_relation AS rp\n" +
                "                           ON r.id = rp.role_id\n" +
                "                 LEFT JOIN permission AS p\n" +
                "                           ON p.id = rp.permission_id\n" +
                "        WHERE u.id = #{userId}"
    )
    fun selectListByUser(userId: Int): List<Permission>?

    /**
     * 查询具体某个接口的权限
     *
     * @param path
     * @return 权限列表
     */
    @Select(
        "   SELECT pe.*\n" +
                "        FROM permission pe\n" +
                "                 LEFT JOIN permission_path_relation re ON re.permission_id = pe.id\n" +
                "                 LEFT JOIN request_path pa ON pa.id = re.path_id\n" +
                "        WHERE pa.url = #{path}"
    )
    fun selectListByPath(path: String): List<Permission>?

    /**
     * 查询权限数量
     *
     * @param
     * @return 权限列表
     */
    @Select(
        "  SELECT COUNT(*)\n" +
                "        FROM permission"
    )
    fun selectTotalPermissions(): Int

    /**
     * 查询角色下具有的所有权限
     *
     * @param  roleId
     * @return 权限列表
     */
    @Select(
        " select p.*\n" +
                "        from permission as p,\n" +
                "             role_permission_relation AS rp\n" +
                "        where rp.role_id = #{roleId}\n" +
                "          and p.id = rp.permission_id"
    )
    fun selectPermissionsByRoleId(roleId: Int): List<Permission>?
}